package cn.edu.hbwe.book.admin.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.edu.hbwe.book.entity.Notice;
import cn.edu.hbwe.book.utils.C3P0Util;
public class NoticeDao {

	private static QueryRunner run = new QueryRunner(C3P0Util.getDataSource());

	// 查询所有的公告
	public List<Notice> getAllNotices() throws SQLException {
		String sql = "select * from notice order by n_time desc limit 0,10";
		return run.query(sql, new BeanListHandler<Notice>(Notice.class));
	}

	// 根据id查找公告
	public Notice findNoticeById(String n_id) throws SQLException {
		String sql = "select * from notice where n_id = ?";
		return run.query(sql, new BeanHandler<Notice>(Notice.class), n_id);
	}

	// 添加公告
	public void addNotice(Notice n) throws SQLException {
		String sql = "insert into notice(title,details,n_time) values(?,?,?)";
		run.update(sql, n.getTitle(), n.getDetails(), n.getN_time());
	}

	// 根据id修改单个公告
	public void updateNotice(Notice n) throws SQLException {
		String sql = "update notice set title=?,details=?,n_time=? where n_id=?";
		run.update(sql, n.getTitle(), n.getDetails(), n.getN_time(), n.getN_id());
	}

	//根据id删除公告
	public void deleteNotice(String n_id) throws SQLException {
		String sql = "delete from notice where n_id = ?";
		run.update(sql, n_id);
	}

}
